#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
查询fund_info和fund_info_detail表的数据条数
"""

import os
import sys
import django  # type: ignore

# 设置Django环境
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wealth_backend.settings_mysql')
django.setup()

from data_service.models import FundInfo, FundInfoDetail
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def check_table_counts():
    """
    查询fund_info和fund_info_detail表的数据条数
    """
    try:
        logger.info("开始查询数据表记录数...")
        
        # 查询fund_info表记录数
        fund_info_count = FundInfo.objects.count()
        logger.info(f"fund_info表记录数: {fund_info_count}")
        
        # 查询fund_info_detail表记录数
        fund_info_detail_count = FundInfoDetail.objects.count()
        logger.info(f"fund_info_detail表记录数: {fund_info_detail_count}")
        
        # 输出汇总信息
        logger.info("=== 数据表记录数汇总 ===")
        logger.info(f"fund_info表: {fund_info_count} 条记录")
        logger.info(f"fund_info_detail表: {fund_info_detail_count} 条记录")
        logger.info(f"总计: {fund_info_count + fund_info_detail_count} 条记录")
        
        return {
            'fund_info': fund_info_count,
            'fund_info_detail': fund_info_detail_count,
            'total': fund_info_count + fund_info_detail_count
        }
        
    except Exception as e:
        logger.error(f"查询数据表记录数失败: {str(e)}")
        return None

def main():
    """
    主函数
    """
    result = check_table_counts()
    
    if result:
        print("\n查询结果:")
        print(f"fund_info表: {result['fund_info']} 条记录")
        print(f"fund_info_detail表: {result['fund_info_detail']} 条记录")
        print(f"总计: {result['total']} 条记录")
        return True
    else:
        print("查询失败")
        return False

if __name__ == '__main__':
    success = main()
    sys.exit(0 if success else 1)